package cn.chaos.bio01;

import java.io.ByteArrayOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class DelimFramer implements Frame {

    private InputStream in;
    
    private static final byte DELIMITER = '\n';
    public static final byte[] EMPTY = new byte[0];
    
    public DelimFramer(InputStream in) {
        this.in = in;
    }
    
    @Override
    public void frameMsg(byte[] message, OutputStream out) throws IOException {
        for (byte b :message) {
            if (b == DELIMITER) {
                throw new IOException("Message contains delimiter!");
            }
        }
        
        out.write(message);
        out.write(DELIMITER);
        out.flush();

    }

    @Override
    public byte[] nextMsg() throws IOException {
       
        ByteArrayOutputStream messageBuffer = new ByteArrayOutputStream();
        int nextByte;
        
        while ((nextByte = in.read()) != DELIMITER) {
            if (nextByte == -1) {
                if (messageBuffer.size() == 0) {
                    return EMPTY;
                } else {
                    throw new EOFException("Non-empty message without delimiter");
                }
            }
            messageBuffer.write(nextByte);
        }
        return messageBuffer.toByteArray();
    }

}
